using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Business;
using Entity;
public partial class UserControl_PlanManager : System.Web.UI.UserControl
{
    ePlan plan;
    String type;
    int PlanID;
    public UserControl_PlanManager()
    {

    }
    public UserControl_PlanManager(String PlanID)
    {
        this.PlanID= int.Parse(PlanID);
        if (this.PlanID != -1)
        {
            type = "Update";
        }
        else
        {
            type = "Addnew";

        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        lbMess.Text = "";

        dlProduct.DataSource = bProduct.getAllProduct();
        dlProduct.DataTextField = "producName";
        dlProduct.DataValueField = "productID";
        dlProduct.DataBind();
        plan = bPlan.getPlanById(PlanID);
        btDelete.Attributes.Add("onclick", "return confirm('Are you Sure you want to delete this Plan')");
        loadForm(type);

    }
    void loadForm(String typeView)
    {
      //  Response.Write("Plan load form");
        if (typeView == "Update")
        {
            btAdd.Text = "Update";
            txtPlanID.Text = plan.planID.ToString();
            txtPlanName.Text = plan.planName;
            txtPlanDescription.Text = plan.planDescription;
            dlProduct.SelectedValue = plan.productID.ToString();
        }
        else
        {
            txtPlanID.Text = "";
            txtPlanName.Text = "";
            txtPlanDescription.Text = "";
            btDelete.Visible=false;
        }
    }

    protected void btReset_Click(object sender, EventArgs e)
    {
        loadForm(type);
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProductManage.aspx?addnew=Plan");
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        if (type == "Update")
        {
            if (plan != null)
            {
                plan.planName = txtPlanName.Text.Trim();
                plan.planDescription = txtPlanDescription.Text.Trim();
                int productID = int.Parse(dlProduct.SelectedValue);
                plan.productID = productID;
                bPlan.updatePlan(plan);
                Response.Redirect("ProductManage.aspx?selectValue=Product." + plan.productID + "/Plan." + plan.planID);
            }
        }
        else
        {
            plan = new ePlan();
            plan.planName = txtPlanName.Text.Trim();
            plan.planDescription = txtPlanDescription.Text.Trim();
            int productID = int.Parse(dlProduct.SelectedValue);
            plan.productID = productID;
            bPlan.insertPlan(plan);
            plan = bPlan.getLastPlan();
            Response.Redirect("ProductManage.aspx?selectValue=Product." + plan.productID + "/Plan." + plan.planID);
        }
    }
    protected void btDelete_Click(object sender, EventArgs e)
    {
        if (plan != null)
        {
            bPlan.deletePlan(plan.planID);
            Response.Write("<Script>alert('you has deleted Plan'); window.location.href='ProductManage.aspx'</Script>"); 
        }
    }
}
